exports.updateOrganizationDiscountActivity = function (req, res) {

    //获取restful参数
    var bodyParams = req.routeInfo.bodyParams;
    //最终结果
    var resultData = {};

    var completeCount = 0;

    var successCount = 0;
    var o_address ="";
    var count = 0;
    //任务流创建
    async.waterfall([
        //1.权限验证
        /*function (cb) {
            P.permissionValidation(req, res, [CORE_APIConfig.coreAPI.updateOrganizationActivity], function () {
                cb()
            });
        },*/

        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchOrganization + "/o_id/"+ bodyParams.oid,
                    method: 'POST',
                    data: {
                        fields: ["o_address"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    o_address = data.data[0].o_address;
                    cb();
                }
            );

        },
            //查询_更新促销活动基本信息
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url:CORE_APIConfig.coreAPI.updateOrganizationActivity,
                        method: 'POST',
                        data: {
                            oa_id           : bodyParams.oa_id,
                            oa_type         : 5,
                            oa_title        : bodyParams.activity_name,
                            oa_oid          : bodyParams.oid,
                            oa_category_text: bodyParams.category_text,
                            oa_start_time   : bodyParams.activity_start_time,
                            oa_end_time     : bodyParams.activity_end_time,
                            oa_explain      : bodyParams.activity_note,
                            oa_title_image  : bodyParams.activity_image,
                            oa_role   : bodyParams.activity_role,
                            oa_reward     : bodyParams.activity_reward,
                            oa_title_image_text      : bodyParams.title_image_text,
                            oa_title_image_text_style      : bodyParams.title_image_text_style,
                            oa_skin_id  : bodyParams.activity_skin_id,
                            oa_music_id  : bodyParams.activity_music_id,
                            oa_class_time  : bodyParams.activity_class_time,
                            oa_class_duration  : bodyParams.activity_class_duration,
                            oa_class_apply  : bodyParams.activity_class_apply,
                            oa_class_people  : bodyParams.activity_class_people,
                            oa_class_basics  : bodyParams.activity_class_basics,
                            oa_class_phone  : bodyParams.activity_class_phone,
                            oa_thumbnail  : bodyParams.oa_thumbnail,
                            oa_title_image_type  : bodyParams.oa_title_image_type,
                        }

                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "促销活动主表更新失败，请重新尝试");
                        }
                        cb();
                    }
                );

            },


            //查询_更新促销活动扩展信息
            function(cb){
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.updateDiscountsActivityConfig,
                        method: 'POST',
                        data: {
                            dac_id           : bodyParams.dac_id,
                            dac_oaid         : bodyParams.oa_id,
                            dac_max_count    : bodyParams.activity_max_count,
                            dac_old_price    : bodyParams.activity_old_price,
                            dac_new_price    : bodyParams.activity_new_price,
                            dac_address      : bodyParams.o_address == undefined ? o_address : bodyParams.o_address,
                            dac_coupon_id    : bodyParams.coupon_id == undefined ? '' : bodyParams.coupon_id,
                            dac_coupon_type  : bodyParams.coupon_type == undefined ? '' : bodyParams.coupon_type,
                            dac_prompt  : bodyParams.dac_prompt == undefined ? '' : bodyParams.dac_prompt,
                        }
                    },
                    function (err, data) {

                        //console.log(data);

                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "促销活动配置表更新失败，请重新尝试");
                        }
                        cb();
                    }
                );

            },

        //查询_更新促销活动扩展信息
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchActivityCoupon + "/ac_oaid/"+bodyParams.oa_id + "/count/1",
                    method: 'POST',
                    fields : [
                        "ac_id",

                    ].join(",")
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return RES.response(res, false, "优惠劵记录查询失败");
                    }
                    count = data.count;
                    cb();
                }
            );

        },


        function(cb){
            if(count>0){
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.deleteActivityCoupon,
                        method: 'POST',
                        data: {
                            ac_oaid               : bodyParams.oa_id,
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "优惠券记录表删除失败");
                        }

                        cb();
                    }
                );
            }else{
                cb();
            }
        },

        function (cb){
            if(bodyParams.coupon_id!= undefined && bodyParams.coupon_id != ''&& bodyParams.coupon_id != 0){
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.addActivityCoupon,
                        method: 'POST',
                        data: {
                            ac_oid       : bodyParams.oid,                //机构id
                            ac_oaid      : bodyParams.oa_id,                      //活动id
                            ac_type      : bodyParams.coupon_type,   //券类型
                            ac_fid       : bodyParams.coupon_id,      //券id
                            ac_oatype    : 2                       //活动类型
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "优惠券记录表更新失败");
                        }

                        cb();
                    }
                );
            }else{
                cb();
            }
        },
            //查询促销活动内容
            function (cb) {
                if(bodyParams.ischangecontent==1){

                    //console.log(CORE_APIConfig.coreAPI.deleteOrganizationDiscountsActivityContent)
                    R.SEND_HTTP(
                        req,{
                            url:CORE_APIConfig.coreAPI.deleteOrganizationDiscountsActivityContent,
                            method:'POST',
                            data:{
                                dac_oaid:bodyParams.oa_id,
                            }
                        },
                        function (err, data) {

                            //console.log("------------");
                            //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, "该活动内容删除失败，请重新尝试");
                            }

                            var contentArr = JSON.parse(bodyParams.activity_content_data);
                            //console.log("---------")
                            //console.log(contentArr);
                            successCount  = contentArr.length;
                            completeCount = 0;
                            //创建添加任务
                            var tasks = [];
                            //console.log(contentArr);
                            //循环节点列表
                            contentArr.forEach(function (item, i) {
                                //创建每一个插入任务
                                tasks.push(function (cb) {
        
                                    R.SEND_HTTP(
                                        req,
                                        {
                                            url: CORE_APIConfig.coreAPI.addDiscountsActivityContent,
                                            method: 'POST',
                                            data: {
                                                dac_type      : item.type,
                                                dac_order     : i,
                                                dac_content   : item.content,
                                                dac_oaid      : bodyParams.oa_id
                                            }
                                        },
                                        function (err, data) {
        
                                            //console.log(data);
        
                                            //判断是否有错误信息
                                            if(err) {
                                                //console.log("添加错误");
                                            }else{
                                                completeCount++;
                                            }
                                            cb();
                                        }
                                    );
                                });
                            });
                            async.waterfall(tasks, cb);


                        }
                    )
                   
                }else {
                    cb();
                }
            },
            // //判断写入结果
            function(cb){
                if(successCount == completeCount){
                    cb();
                }else{
                    return RES.response(res, false, "活动内容新增失败，请重新尝试");
                }
            }

        ], function (err, result) {
            //返回最终拼接结果
            RES.response(res, true, resultData);
        }
    );
}